package com.wenhai.disruptor;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import com.wenhai.disruptor.event.OrderEvent;
import com.wenhai.disruptor.event.OrderEventFactory;
import com.wenhai.disruptor.handler.OrderEventHandler;
import com.wenhai.disruptor.producer.OrderEventProducer;

import java.nio.ByteBuffer;
import java.util.concurrent.ThreadFactory;

/**
 *
 *
 * @author wenhai
 * @date   2021/1/11
 */
public class Main1 {
    public static void main(String[] args) {
        Disruptor<OrderEvent> disruptor = new Disruptor<>(new OrderEventFactory(),
                1024 * 1024,
                (ThreadFactory) Thread::new,
                ProducerType.SINGLE,
                new BlockingWaitStrategy());
        disruptor.handleEventsWith(new OrderEventHandler());
        disruptor.start();


        // 获取实际存储数据的容器： RingBuffer
        RingBuffer<OrderEvent> rb = disruptor.getRingBuffer();
        OrderEventProducer orderEventProducer = new OrderEventProducer(rb);

        ByteBuffer bb = ByteBuffer.allocate(100);
        for (int i = 0; i < 100; i++) {
            bb.putLong(0, i);
            orderEventProducer.sendData(bb);

        }
        System.out.println("=====");
        disruptor.shutdown();
    }
}
